package com.yfl.common.handler;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import com.yfl.common.CodeMsg;
import com.yfl.common.Result;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 拦截从Controller里面来的异常
 * 要让它生效,必须有这个对象
 * 我们需要在spring-mvc.xml文件里面包扫描
// * @see classpath:spring-mvc.xml
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 处理shiro的登陆异常
     */
    @ExceptionHandler(value = ShiroException.class)
    public Result loginException(ShiroException shiroException){
        /*// 用户不存在
        if (shiroException instanceof UnknownAccountException){
            return Result.fail(400,"没有该用户");
        }
        // 用户密码错误
        if (shiroException instanceof IncorrectCredentialsException){
            return Result.fail(400,"密码错误");
        }
        // 没有权限
        if (shiroException instanceof UnauthenticatedException) {
            return Result.fail(400,"没有权限访问");
        }*/
        if (shiroException instanceof AuthenticationException){
            return Result.fail(CodeMsg.USER_USER_PASSWORD_ERROR.code, CodeMsg.USER_USER_PASSWORD_ERROR.msg);
        }
        return Result.fail();
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public Result handlerIllegalArgumentException(IllegalArgumentException exception) {
        String message = exception.getMessage();
        System.out.println(message);
        return Result.fail(400, message);
    }

}
